package io.geekidea.boot.test.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.geekidea.boot.framework.exception.BusinessException;
import io.geekidea.boot.framework.page.OrderByItem;
import io.geekidea.boot.framework.page.OrderMapping;
import io.geekidea.boot.framework.page.Paging;
import io.geekidea.boot.test.dto.ClassesDto;
import io.geekidea.boot.test.entity.Classes;
import io.geekidea.boot.test.mapper.ClassesMapper;
import io.geekidea.boot.test.query.ClassesQuery;
import io.geekidea.boot.test.service.ClassesService;
import io.geekidea.boot.test.vo.ClassesVo;
import io.geekidea.boot.test.query.AppClassesQuery;
import io.geekidea.boot.test.vo.AppClassesVo;
import io.geekidea.boot.util.PagingUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.List;

/**
 *  服务实现类
 *
 * @author geekidea
 * @since 2025-02-19
 */
@Slf4j
@Service
public class ClassesServiceImpl extends ServiceImpl<ClassesMapper, Classes> implements ClassesService {

    @Autowired
    private ClassesMapper classesMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean addClasses(ClassesDto dto) {
        Classes classes = new Classes();
        BeanUtils.copyProperties(dto, classes);
        return save(classes);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateClasses(ClassesDto dto) {
        Long id = dto.getId();
        Classes classes = getById(id);
        if (classes == null) {
            throw new BusinessException("不存在");
        }
        BeanUtils.copyProperties(dto, classes);
        return updateById(classes);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteClasses(Long id) {
        return removeById(id);
    }

    @Override
    public ClassesVo getClassesById(Long id) {
        return classesMapper.getClassesById(id);
    }

    @Override
    public Paging<ClassesVo> getClassesPage(ClassesQuery query) {
        OrderMapping orderMapping = new OrderMapping();
        PagingUtil.handlePage(query, orderMapping, OrderByItem.desc("id"));
        List<ClassesVo> list = classesMapper.getClassesPage(query);
        Paging<ClassesVo> paging = new Paging<>(list);
        return paging;
    }

    @Override
    public AppClassesVo getAppClassesById(Long id) {
        return classesMapper.getAppClassesById(id);
    }

    @Override
    public Paging<AppClassesVo> getAppClassesPage(AppClassesQuery query) {
        OrderMapping orderMapping = new OrderMapping();
        PagingUtil.handlePage(query, orderMapping, OrderByItem.desc("id"));
        List<AppClassesVo> list = classesMapper.getAppClassesPage(query);
        Paging<AppClassesVo> paging = new Paging<>(list);
        return paging;
    }

}
